/**
 * 
 */
package com.dalonedrau.jogl.engine;

import java.util.ArrayList;

import com.dalonedrau.watchable.Watchable;
import com.dalonedrau.watchable.Watcher;

/**
 * @author Donald
 */
public final class GameState implements Watchable {
	/** the one and only instance of the <code>GameState</code> class. */
	private static GameState	instance;
	/** game initializing. */
	public static final int		STATE_00INITIALIZING		= 0;
	/** start menu. */
	public static final int		STATE_01STARTMENU			= 1;
	/** game tutorial. */
	public static final int		STATE_02TUTORIAL			= 2;
	/** game in-play. */
	public static final int		STATE_03IN_PLAY				= 3;
	/** game over. */
	public static final int		STATE_04GAME_OVER			= 4;
	/** game paused. */
	public static final int		STATE_05PAUSED				= 5;
	/** game between levels. */
	public static final int		STATE_06BETWEEN_LEVELS		= 6;
	/** game ended. */
	public static final int		STATE_07GAME_ENDED			= 7;
	/** confirm user wants to exit. */
	public static final int		STATE_08CONFIRM_EXIT		= 8;
	/** game options menu. */
	public static final int		STATE_09GAME_OPTIONS_MENU	= 9;
	/** demo mode. */
	public static final int		STATE_10DEMO_MODE			= 10;
	/** demo mode. */
	public static final int		STATE_11LOADING				= 11;
	/** demo mode. */
	public static final int		STATE_12WORLD_MAP			= 12;
	/** demo mode. */
	public static final int		STATE_13SB_CHAR_CREATOR		= 13;
	/** demo mode. */
	public static final int		STATE_14HQ_CHAR_CHOOSER		= 14;
	/** demo mode. */
	public static final int		STATE_15HQ_DUNGEON_SPLASH	= 15;
	/** demo mode. */
	public static final int		STATE_16HQ_DUNGEON_SCREEN	= 16;
	/** combat mode. */
	public static final int		STATE_17HQ_IN_COMBAT	= 16;
	/**
	 * Gives access to the singleton instance of {@link GameState}.
	 * @return {@link GameState}
	 */
	public static GameState getInstance() {
		if (GameState.instance == null) {
			GameState.instance = new GameState();
		}
		return GameState.instance;
	}
	/** This data member will store the current state. */
	private int					currentState;
	/** the list of watchers. */
	private ArrayList<Watcher>	watchers;
	/** Hidden constructor. */
	private GameState() {}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void addWatcher(final Watcher watcher) {
		if (watchers == null) {
			watchers = new ArrayList<Watcher>(100);
		}
		watchers.add(watcher);
	}
	/**
	 * Gets the current state.
	 * @return {@link int}
	 */
	public int getCurrentState() {
		return currentState;
	}

	public void nextState() {
		currentState++;
		notifyWatchers();
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void notifyWatchers() {
		if (watchers != null) {
			for (int i = 0; i < watchers.size(); i++) {
				watchers.get(i).watchUpdated(this);
			}
		}
	}
	public void previousState() {
		currentState--;
		notifyWatchers();
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void removeWatcher(final Watcher watcher) {
		if (watchers != null) {
			watchers.remove(watcher);
		}
	}
	/**
	 * Sets the current state.
	 * @param value the value to set
	 */
	public void setCurrentState(final int value) {
		currentState = value;
		notifyWatchers();
	}
}
